﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections;
using EnvDTE;
using System.IO;
using System.Drawing.Design;

namespace ProgNetComponents.Design.Serialization
{
    public sealed class ProjectHelper
    {
        public static void AddConstantStringClass(IComponent Comp,
            string Namespace,
            string ClassName,
            IList List,
            string PropertyName
            )
        {
            List<string> list = new List<string>();
            string str = "";
            foreach (object o in List)
            {
                str = o.GetPropertyValue(PropertyName).AsString();
                list.Add(str);
            }
            AddConstantStringClass(Comp, Namespace, ClassName, list);
        }

        public static void AddConstantStringClass(IComponent Comp,
            string Namespace,
            string ClassName,
            List<string> strings)
        {
            if (Comp != null && Comp.Site != null)
            {
                AddConstantStringClass(Comp.Site, Namespace, ClassName, strings);
            }
        }

        public static void AddConstantStringClass(IServiceProvider srvProvider,
           string Namespace,
           string ClassName,
           List<string> strings)
        {
            if (srvProvider != null)
            {
                ProjectItem projectItem = srvProvider.GetService(typeof(ProjectItem)) as ProjectItem;
                if (projectItem != null)
                {
                    string filePath = Path.GetDirectoryName(projectItem.Document.Path);
                    string file = filePath + "\\" + ClassName + ".cs";

                    FileStream fs = System.IO.File.Open(file, System.IO.FileMode.Create);
                    System.IO.StreamWriter sw = new System.IO.StreamWriter(fs);
                    sw.WriteLine("namespace " + Namespace);
                    sw.WriteLine("{");
                    sw.WriteLine("\tpublic static class " + ClassName);
                    sw.WriteLine("\t{");
                    sw.WriteLine("\t\tpublic static System.Collections.Generic.Dictionary<string, object> StaticCollection = new System.Collections.Generic.Dictionary<string, object>();");
                    string name = "";
                    string value = "";
                    foreach (string o in strings)
                    {
                        name = GetName(o);
                        if (o.IsNotNull())
                        {
                            value = o.Replace(@"\", @"\\");
                            sw.WriteLine();
                            sw.WriteLine("\t\t/// <summary>");
                            sw.WriteLine("\t\t/// " + value);
                            sw.WriteLine("\t\t/// </summary>");
                            sw.WriteLine("\t\tpublic const string " + name + " = \"" + value + "\";");
                        }
                    }
                    sw.WriteLine("\t\tpublic static void Refresh()");
                    sw.WriteLine("\t\t{");
                    sw.WriteLine("\t\t\tStaticCollection.Clear();");
                    foreach (string o in strings)
                    {
                        name = GetName(o);
                        if (o.IsNotNull())
                        {
                            value = o.Replace(@"\", @"\\");
                            sw.WriteLine("\t\t\tStaticCollection.Add(\"" + name + "\", " + name + ");");
                        }
                    }
                    sw.WriteLine("\t\t}");
                    sw.WriteLine("\t}");
                    sw.WriteLine("}");
                    sw.Flush();
                    sw.Close();
                    fs.Close();
                    ProjectItem item = null;
                    try
                    {
                        projectItem.ContainingProject.ProjectItems.Item(ClassName + ".cs");
                    }
                    catch { }
                    if (item == null)
                        projectItem.ContainingProject.ProjectItems.AddFromFile(file);
                }
            }
        }

        public static string GetName(string value)
        {
            string name = value.Trim().XReplace("\\s+", "_");
            name = name.XReplace("[^a-zA-Z0-9_]+", "_");

            if (!name.XMatch("^[a-zA-Z_]"))
                name = "_" + name;
            return name;
        }

    }
}
